<?php
namespace App\Classes\DataExport;

// use App\Models\Application\ColumnRecommend;
// use App\Models\Application\SubcolumnRecommend;
// use App\Models\Entity\Asset\Video\Vasseti;
// use App\Models\Entity\Asset\Video\Vseriali;
// use App\Models\Entity\Column\Column;
// use App\Models\Entity\Column\Data;
// use App\Models\Entity\Column\Subcolumn;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;

class DataExport
{
    // 文件系统
    public $filesystem;
    public $storage;
    // 接口项目根目录
    protected $path;
    // 数据发布目录
    protected $releasePath;
    // 媒资数据发布目录
    protected $vassetiReleasePath;
    // 分集数据发布目录
    protected $vserialiReleasePath;
    // 栏目数据发布目录
    protected $columnReleasePath;
    // 二级栏目数据发布目录
    protected $subcolumnReleasePath;
    // 一级栏目推荐数据发布目录
    protected $columnRecommendReleasePath;
    // 二级栏目推荐数据发布目录
    protected $subcolumnRecommendReleasePath;
    // 栏目列表数据发布目录
    protected $collistReleasePath;
    public function __construct()
    {
        // 发布文件路径配置
        $this->filesystem = new Filesystem;
        $this->storage    = new Storage;
        // 接口文件根目录
        $this->path                          = config('custom.base.xiaole_api');
        $this->releasePath                   = config('custom.base.xiaole_api.release_base_path');
        $this->vassetiReleasePath            = config('custom.base.xiaole_api.vasseti.path');
        $this->vserialiReleasePath           = config('custom.base.xiaole_api.vseriali.path');
        $this->columnReleasePath             = config('custom.base.xiaole_api.column.path');
        $this->subcolumnReleasePath          = config('custom.base.xiaole_api.subcolumn.path');
        $this->columnRecommendReleasePath    = config('custom.base.xiaole_api.column_recommend.path');
        $this->subcolumnRecommendReleasePath = config('custom.base.xiaole_api.subcolumn_recommend.path');
        $this->collistReleasePath            = config('custom.base.xiaole_api.collist.path');
        $this->release();
    }

    /**
     * 发布数据
     * @return [type] [description]
     */
    public function release()
    {
        // 判断接口目录是否存在
        if (!is_dir($this->releasePath)) {
            $this->filesystem->makeDirectory($this->releasePath);
        }

        // 判断媒资目录是否存在
        if (!is_dir($this->vassetiReleasePath)) {
            $this->filesystem->makeDirectory($this->vassetiReleasePath);
        }
        // 判断分集目录是否存在
        if (!is_dir($this->vserialiReleasePath)) {
            $this->filesystem->makeDirectory($this->vserialiReleasePath);
        }
        // 判断栏目目录是否存在
        if (!is_dir($this->columnReleasePath)) {
            $this->filesystem->makeDirectory($this->columnReleasePath);
        }
        // 判断二级栏目目录是否存在
        if (!is_dir($this->subcolumnReleasePath)) {
            $this->filesystem->makeDirectory($this->subcolumnReleasePath);
        }
        // 判断一级栏目目录是否存在
        if (!is_dir($this->columnRecommendReleasePath)) {
            $this->filesystem->makeDirectory($this->columnRecommendReleasePath);
        }
        // 判断二级栏目目录是否存在
        if (!is_dir($this->subcolumnRecommendReleasePath)) {
            $this->filesystem->makeDirectory($this->subcolumnRecommendReleasePath);
        }

        // 判断
        if (!is_dir($this->collistReleasePath)) {
            $this->filesystem->makeDirectory($this->collistReleasePath);
        }
        return true;
    }

    /**
     * 遍历数据写入JSON
     */
    public function writeJsonData($data = [], $type)
    {

        foreach ($data as $key => $value) {
            switch ($type) {
                case 'vasseti':
                    $this->filesystem->put($this->vassetiReleasePath . DIRECTORY_SEPARATOR . 'vasseti_' . $value['vasseti_id'] . '.json', json_encode($value));
                    break;
                case 'vseriali':
                    $this->filesystem->put($this->vserialiReleasePath . DIRECTORY_SEPARATOR . 'vseriali_' . $value['vseriali_id'] . '.json', json_encode($value));
                    break;
                case 'column':
                    $this->filesystem->put($this->columnReleasePath . DIRECTORY_SEPARATOR . 'column_' . $value['column_id'] . '.json', json_encode($value));
                    break;
                case 'subcolumn':
                    $this->filesystem->put($this->subcolumnReleasePath . DIRECTORY_SEPARATOR . 'subcolumn_' . $value['subcolumn_id'] . '.json', json_encode($value));
                    break;
                case 'column_recommend':
                    $this->filesystem->put($this->columnRecommendReleasePath . DIRECTORY_SEPARATOR . 'column_recommend_' . $value['column_id'] . '.json', json_encode($value));
                    break;
                case 'subcolumn_recommend':
                    $this->filesystem->put($this->subcolumnRecommendReleasePath . DIRECTORY_SEPARATOR . 'subcolumn_recommend_' . $value['subcolumn_id'] . '.json', json_encode($value));
                    break;
                case 'collist':
                    $this->filesystem->put($this->collistReleasePath . DIRECTORY_SEPARATOR . 'collist_' . $value['collist_id'] . '.json', json_encode($value));
                    break;
                default:
                    # code...
                    break;
            }
        }
    }
}
